// components/logistics/booking-space/booking-space.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: 'USA', value: '报关', checked: false },
      { name: 'CHN', value: '拖车', checked: false, address: false },
      {
        name: 'BRA',
        value: '保险',
        checked: false,
        freight: 0,
        currency: ['人名币', '美元', '英镑', '卢布', '越南盾', '日元', '韩元'],
        currentCurrency: ''
      }
    ],
    // 柜型下拉里面的数据
    cabinetList: [
      { cabinetName: '20GP', number: 0, weight: 0 },
      { cabinetName: '40GP', number: 0, weight: 0 },
      { cabinetName: '20HQ', number: 0, weight: 0 }
    ],
    addressData: [
      {
        label: '北京',
        value: '110000',
        children: [
          {
            label: '北京市',
            value: '110000',
            children: [
              {
                label: '东城区',
                value: '110101'
              }
            ]
          }
        ]
      },
      {
        label: '上海',
        value: '310000',
        children: [
          {
            label: '上海市',
            value: '310000',
            children: [
              {
                label: '黄浦区',
                value: '310101'
              }
            ]
          }
        ]
      }
    ],
    cabinetVisible: false, // 柜型下拉框
    addressVisible: false, // 地址下拉框
    currencyVisible: false, // 币种下拉框
    loadingTimeVisible: false, // 时间选择下拉框
    show: false, // 时间选择下拉框

    value3: '',
    list: ['a', 'b', 'c'],
    result: ['a', 'b'],

    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2019, 10, 1).getTime(),
    currentDate: new Date().getTime()
  },
  onChange (event) {
    this.setData({
      result: event.detail
    })
  },

  handleServices (e) {
    let idx = e.target.dataset.index
    this.data.items[idx]['checked'] = !this.data.items[idx]['checked']
    this.setData({
      items: this.data.items
    })
    console.log(this.data.items, '---------------')
  },

  // handleCabinetVisible (e) {
  //   this.setData({
  //     cabinetVisible: true
  //   })
  // },
  // handleCabinetClose (e) {
  //   this.setData({
  //     cabinetVisible: false
  //   })
  // },
  // onOpenAddress () {
  //   this.setData({ addressVisible: true })
  // },
  // onAddressClose () {
  //   this.setData({ addressVisible: false })
  // },

  // todo 统一处理可见的
  handleVisible (e) {
    this.setData({
      [e.currentTarget.dataset.namespace]: true
    })
  },

  // todo 统一处理隐藏
  handleInvisible (e) {
    this.setData({
      [e.currentTarget.dataset.namespace]: false
    })
  },

  handelCabinetReset (e) {
    console.log(e)
    var transfer = [
      { cabinetName: '20GP', number: 0, weight: 0 },
      { cabinetName: '40GP', number: 0, weight: 0 },
      { cabinetName: '20HQ', number: 0, weight: 0 }
    ]
    this.setData({
      cabinetList: transfer
    })
  },

  handleNumberChange (e) {
    let idx = e.target.dataset.index
    this.data.cabinetList[idx]['number'] = e.detail.value
    this.setData({
      cabinetList: this.data.cabinetList
    })
    console.log(e.detail, this.data.cabinetList, '数据绑定流')
  },

  handleCabinetChange (e) {
    let idx = e.target.dataset.index
    this.data.cabinetList[idx]['weight'] = e.detail.value
    this.setData({
      cabinetList: this.data.cabinetList
    })
    console.log(e.detail, this.data.cabinetList, '数据绑定流')
  },

  handleInsuranceChange (e) {
    this.data.items[1]['freight'] = parseFloat(e.detail.value)
    this.setData({
      items: this.data.items
    })
    console.log(e.detail, this.data.items, '数据绑定流')
  },

  onChangeAddress (e) {
    console.log('onChangeAddress', e.detail)
    this.data.items[1]['address'] =
      e.detail.done && e.detail.options.map(n => n.label).join('/')
    this.setData({
      value2: e.detail.value,
      items: this.data.items
    })
  },
  onLoadOptions (e) {
    console.log('onLoadOptions', e.detail)
    const { value } = e.detail
    const options2 = [...this.data.options2]

    wx.showLoading({ mask: true })

    setTimeout(() => {
      if (value[value.length - 1] === 'beijing') {
        options2.forEach(n => {
          if (n.value === 'beijing') {
            n.children = [
              {
                value: 'baidu',
                label: '百度'
              },
              {
                value: 'sina',
                label: '新浪'
              }
            ]
          }
        })
      } else if (value[value.length - 1] === 'hangzhou') {
        options2.forEach(n => {
          if (n.value === 'hangzhou') {
            n.children = [
              {
                value: 'ali',
                label: '阿里巴巴'
              },
              {
                value: '163',
                label: '网易'
              }
            ]
          }
        })
      }

      wx.hideLoading()

      this.setData({ value2: value, options2 })
    }, 10)
  },

  showPopup () {
    this.setData({ loadingTimeVisible: true })
  },

  onClose () {
    this.setData({ loadingTimeVisible: false })
  },

  onVisibleChange (e) {
    this.setData({ visible: e.detail.visible })
  },

  onValueChange (e) {
    const { index } = e.currentTarget.dataset
    console.log(`onValueChange${index}`, e.detail)
  },

  onConfirm (e) {
    const { index } = e.currentTarget.dataset
    this.data.items[2].currentCurrency = e.detail.label
    this.setData({
      items: this.data.items,
      [e.currentTarget.dataset.namespace]: false,
      [`value${index}`]: e.detail.value,
      [`displayValue${index}`]: e.detail.label,
    })
    // this.setValue(e.detail, index)
    console.log(`onConfirm${index}`, e.detail, this.data)
  },

  // setValue (values, key) {
  //   this.setData({
  //     [`value${key}`]: values.value,
  //     [`displayValue${key}`]: values.label
  //   })
  // },

  /**
   * 日期选择器
   */
  onInput (event) {
    console.log(event.detail)
    this.setData({
      currentDate: this.timestampToTime(event.detail) // 很方便的将时间戳转换成了xxxx-xx-xx xx:xx:xx
    })
  },

  /**
   * 时间戳转换为日期
   */
  timestampToTime (timestamp) {
    var date = new Date(timestamp) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-'
    var M =
      (date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1) + '-'
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    var h =
      (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    var m =
      (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
      ':'
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s
  },

  /**
   * form表单获取
   */
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})
